<template>
  <div class="w">
    <div class="header">
      <i class="iconfont icon-fanhui" @click="back"></i>
      <span class="title">报名提交</span>
    </div>
    <div class="info">
      <div class="form">
        <el-form ref="signUpRef" :model="signUpForm" :rules="signUpRules">
          <el-form-item prop="signUpName" label="姓名">
            <el-input v-model="signUpForm.signUpName" clearable placeholder="请输入姓名" class="input"></el-input>
          </el-form-item>
          <el-form-item prop="signUpStuCard" label="学号">
            <el-input v-model="signUpForm.signUpStuCard" clearable placeholder="请输入学号" class="input"></el-input>
          </el-form-item>
          <el-form-item prop="signUpSex" label="性别">
            <el-radio v-model="signUpForm.signUpSex" label="0">男</el-radio>
            <el-radio v-model="signUpForm.signUpSex" label="1">女</el-radio>
          </el-form-item>
          <el-form-item prop="signUpPhone" label="联系方式">
            <el-input v-model="signUpForm.signUpPhone" placeholder="请输入联系方式" clearable="" class="input"></el-input>
          </el-form-item>
          <el-form-item prop="signUpWeChat" label="微信">
            <el-input v-model="signUpForm.signUpWeChat" placeholder="请输入微信号" clearable="" class="input"></el-input>
          </el-form-item>
          <el-form-item prop="signUpQQ" label="QQ">
            <el-input v-model="signUpForm.signUpQQ" placeholder="请输入QQ号" clearable="" class="input"></el-input>
          </el-form-item>
          <el-form-item prop="signUpClass" label="专业班级">
            <el-input v-model="signUpForm.signUpClass" clearable placeholder="请输入专业班级 如:软件工程2121" class="input"></el-input>
          </el-form-item>
          <el-form-item prop="signUpBirthday" label="出生年月日">
            <el-date-picker
              style="width: 100%"
              v-model="signUpForm.signUpBirthday"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
          <el-form-item prop="signUpIdCard" label="身份证">
            <el-input v-model="signUpForm.signUpIdCard" clearable placeholder="请输入身份证号码" class="input"></el-input>
          </el-form-item>
          <el-form-item prop="signUpPolitical" label="政治面貌">
            <el-select v-model="signUpForm.signUpPolitical" clearable placeholder="请选择" style="width: 100%">
              <el-option v-for="item in politicalOptions" :key="item" :label="item" :value="item"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="signUpNation" label="民族">
            <el-select v-model="signUpForm.signUpNation" clearable placeholder="请选择" style="width: 100%">
              <el-option v-for="item in nationOptions" :key="item" :label="item" :value="item"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="signUpHealthy" label="身体健康状况">
            <el-select
              style="width: 100%"
              v-model="signUpForm.signUpHealthy"
              clearable
              placeholder="请选择"
              v-if="signUpForm.signUpHealthy != '其他疾病'"
            >
              <el-option v-for="item in healthyOptions" :key="item" :label="item" :value="item"></el-option>
            </el-select>
            <el-input
              v-if="signUpForm.signUpHealthy == '其他疾病'"
              v-model="signUpForm.signUpHealthy"
              clearable
              placeholder="请输入疾病名称"
              class="input"
            ></el-input>
          </el-form-item>
          <el-form-item prop="signUpIntroduction" label="自我简介">
            <el-input type="textarea" v-model="signUpForm.signUpIntroduction" clearable placeholder="请输入简介" class="input"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="button" v-if="signUpId == null">
        <el-button @click="summit">提交</el-button>
      </div>
      <div class="button" v-if="signUpId != null">
        <el-button @click="edit">保存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    // 手机号验证规则
    var checkPhone = (rule, value, cb) => {
      const regPhone = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if (regPhone.test(value)) {
        return cb();
      }
      cb(new Error('手机号不合法'));
    };
    var checkIdCard = (rule, value, cb) => {
      const regIdCard = /^[1-8]{2}[0-9]{4}[0-9]{4}((0[1-9]{1})|(1[0-2]{1}))((0[1-9]{1})|(1[0-9]{1})|(2[0-9]{1})|(3[0-1]{1}))[0-9]{3}[0-9xX]{1}$/;
      if (regIdCard.test(value)) {
        return cb();
      }
      cb(new Error('身份证号不合法'));
    };
    return {
      politicalOptions: ['中共党员', '共青团员', '群众'],
      healthyOptions: ['健康', '良好', '其他疾病'],
      nationOptions: [
        '汉族',
        '蒙古族',
        '满族',
        '朝鲜族',
        '赫哲族',
        '达斡尔族',
        '鄂温克族',
        '鄂伦春族',
        '回族',
        '东乡族',
        '土族',
        '撒拉族',
        '保安族',
        '裕固族',
        '维吾尔族',
        '哈萨克族',
        '柯尔克孜族',
        '锡伯族',
        '塔吉克族',
        '乌孜别克族',
        '俄罗斯族',
        '塔塔尔族',
        '藏族',
        '门巴族',
        '珞巴族',
        '羌族',
        '彝族',
        '白族',
        '哈尼族',
        '傣族',
        '僳僳族',
        '佤族',
        '拉祜族',
        '纳西族',
        '景颇族',
        '布朗族',
        '阿昌族',
        '普米族',
        '怒族',
        '德昂族',
        '独龙族',
        '基诺族',
        '苗族',
        '布依族',
        '侗族',
        '水族',
        '仡佬族',
        '壮族',
        '瑶族',
        '仫佬族',
        '毛南族',
        '京族',
        '土家族',
        '黎族',
        '畲族',
        '高山族',
      ],
      signUpForm: {
        signUpName: '',
        signUpPhone: '',
        signUpClass: '',
        signUpIdCard: '',
        signUpSex: '',
        signUpPolitical: '',
        signUpNation: '',
        signUpBirthday: '',
        signUpHealthy: '',
        signUpIntroduction: '',
        jobId: '',
        signUpWeChat: '',
        signUpQQ: '',
        signUpStuCard: '',
      },
      info: [],
      signUpRules: {
        signUpName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        signUpPhone: [
          { validator: checkPhone, trigger: 'blur' },
          { required: true, message: '手机号不能为空', trigger: 'blur' },
        ],
        signUpClass: [{ required: true, message: '请输入专业班级', trigger: 'blur' }],
        signUpIdCard: [
          { validator: checkIdCard, trigger: 'blur' },
          { required: true, message: '身份证号不能为空', trigger: 'blur' },
        ],
        signUpSex: [{ required: true, message: '性别不能为空', trigger: 'blur' }],
        signUpPolitical: [{ required: true, message: '请选择政治面貌', trigger: 'blur' }],
        signUpNation: [{ required: true, message: '请选择民族', trigger: 'blur' }],
        signUpBirthday: [{ required: true, message: '请选择出生日期', trigger: 'blur' }],
        signUpHealthy: [{ required: true, message: '请选择健康状况', trigger: 'blur' }],
        signUpIntroduction: [{ required: true, message: '请输入自我简介', trigger: 'blur' }],
        signUpWeChat: [{ required: true, message: '请输入微信号', trigger: 'blur' }],
        signUpQQ: [{ required: true, message: '请输入QQ号', trigger: 'blur' }],
        signUpStuCard: [{ required: true, message: '请输入学号', trigger: 'blur' }],
      },
      signUpId: '',
    };
  },

  created() {
    this.info = JSON.parse(window.localStorage.getItem('userInfo'));
    this.signUpForm.jobId = this.$route.query.jobId;
    this.signUpId = this.$route.query.signUpId;
    if (this.signUpId != null) {
      this.getSignUp();
    }
  },

  methods: {
    async edit() {
      const { data: res } = await this.$http.put('signUp/updateSignUp', this.signUpForm);
      if (res.code == 1) {
        this.$router.go(-1);
        return this.$message.success(res.data);
      } else {
        this.$message.error('保存失败');
      }
    },
    async getSignUp() {
      const { data: res } = await this.$http.get('signUp/getSignUpOne/' + this.signUpId);
      if (res.code == 1) {
        this.signUpForm = res.data;
        this.signUpForm.signUpSex = JSON.stringify(this.signUpForm.signUpSex);
        return;
      } else {
        return this.$message.error('获取信息失败');
      }
    },
    back() {
      this.$router.go(-1);
    },
    summit() {
      this.$refs.signUpRef.validate(async (valid) => {
        if (valid) {
          this.signUpForm.userId = this.info.id;
          console.log(this.signUpForm);
          const { data: res } = await this.$http.post('signUp/addSignUp', this.signUpForm);
          if (res.code == 1) {
            this.$message.success(res.data);
            this.$router.go(-1);
          } else {
            return this.$message.error('提交失败');
          }
        } else {
          return this.$message.error('表单校验失败');
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.w {
  height: 100vh;
  width: 100%;
  .header {
    background-color: #fece4e;
    height: 50px;
    color: #fff;
    text-align: center;
    position: relative;
    .icon-fanhui {
      position: absolute;
      font-size: 25px;
      line-height: 50px;
      left: 10px;
      font-weight: bold;
    }
    .title {
      font-size: 20px;
      line-height: 50px;
      font-weight: bold;
    }
  }
  .info {
    height: calc(100vh - 80px);
    overflow: auto;
    padding: 15px;
    scrollbar-width: none;
    -ms-overflow-style: none;
    .form {
      :deep(.el-form-item__label) {
        font-size: 16px;
        color: #222c3a;
        font-weight: 530;
        letter-spacing: 2px;
        height: 35px;
      }
      :deep(.el-form-item) {
        margin-bottom: 15px;
      }
      :deep(.el-textarea__inner) {
        height: 80px;
      }
    }
  }
  .button {
    margin-top: 70px;
    text-align: center;
    :deep(.el-button) {
      height: 50px;
      background-color: #5e7aee;
      border-radius: 50px;
      border: none;
      color: #fefefe;
      width: 100%;
      font-size: 17px;
    }
  }
}
</style>